<template>
    <div class="box">
        <!-- 顶部导航栏 -->
        <div class="nav">
            <van-nav-bar title="用户" @click-right="onClickRight">
                <template #left>
                    <van-icon name="setting-o" size="20" color="red" />
                </template>
                <template #right>
                    <p style="color:red">注销</p>
                </template>
            </van-nav-bar>

        </div>

        <!-- 用户中心 -->
        <div class="user">
            <div class="userImg">
                <img src="../../assets/user.png" alt="">
            </div>
            <div class="userInfo">
                <div v-if="!user.username">
                    <h1 @click="tologin">请先登录</h1>
                </div>
                <div v-else>
                    <h1>{{ user.nickname }}</h1>
                    <p>账号：{{ user.username }}</p>
                </div>
            </div>
            <div class="set"><van-icon name="arrow" size="20" color="#ccc" @click="toDetail" /></div>
        </div>

        <!-- 我的工具 -->
        <p class="sideTitle">
            <b>|</b>
            我的工具
        </p>
        <van-grid>
            <van-grid-item icon="friends-o" text="我的好友" />
            <van-grid-item icon="star-o" text="我的收藏" />
            <van-grid-item icon="location-o" text="地址管理" @click="toAddressList" />
            <van-grid-item icon="todo-list-o" text="浏览记录" />
        </van-grid>

        <!-- 我的订单 -->
        <p class="sideTitle">
            <b>|</b>
            我的订单
        </p>
        <van-cell-group inset>
            <van-cell title="我的订单" is-link />
            <van-cell title="发票助手" is-link />
            <van-cell title="联系客服" is-link />
            <van-cell title="帮助反馈" is-link />
        </van-cell-group>
    </div>
</template>

<script>
import Vue from 'vue';
import { NavBar } from 'vant';
import { Icon } from 'vant';
import { Grid, GridItem } from 'vant';
import { Cell, CellGroup } from 'vant';
import { getCookie, removeCookie } from '@/utils/cookie'
import { Dialog } from 'vant';
import {getUser} from '@/request/request'
import store from '@/store';

Vue.use(Dialog);
Vue.use(Cell);
Vue.use(CellGroup);
Vue.use(Grid);
Vue.use(GridItem);
Vue.use(Icon);
Vue.use(NavBar);
export default {
    data() {
        return {
            user: {},
            username: '',
            nickname: '',
        }
    },
    methods: {
        // 退出登录
        onClickRight() {
            if (!this.userView) {
                Dialog.confirm({
                    title: '注销',
                    message: '你确定要退出登录吗？',
                })
                    .then(() => {
                        // on confirm
                        removeCookie('username')
                        removeCookie('nickname')
                        this.$store.commit('uploadUser', {})
                        location.reload()
                        this.userView = false
                    })
                    .catch(() => {
                        // on cancel
                    });
            } else {
                Dialog({ message: '请先登录' });
            }

        },
        tologin() {
            this.$router.push({ path: '/login' });
        },
        toDetail() {
            if (this.username) {
                this.$router.push('/my/info')
            }
        },
        //获取用户信息
        async getUser() {
            let username = getCookie('username');
            if (username) {
                this.userView = false
                var arr = await getUser(username)
                this.user = arr[0]
                // this.user = user
                this.$store.commit('uploadUser', this.user)
                this.username = this.user.username
            }
        },

        //去地址管理页
        toAddressList() {
            this.$router.push('/my/address');
        },
    },
    mounted() {
        this.getUser();
        if (!getCookie('username')) {

        }
    }
}

</script>

<style lang="scss" scoped>
.box {
    background: rgb(247, 247, 247);
    text-align: left;
    height: 100%;
    box-shadow: 0px 0px 15px #efefef;
}

.user {
    border-radius: 10px;
    background-color: #fff;
    display: flex;
    margin: 10px;
    width: 355px;
    height: 100px;
    align-items: center;

    .userImg {
        width: 70px;
        height: 70px;
        margin-right: 30px;


        img {
            width: 70px;
            height: 70px;
        }
    }

    .set {
        float: right;
        margin: 15px;
    }
}

.sideTitle {
    margin-left: 15px;
    color: rgb(142, 142, 142);
    line-height: 15px;
    margin: 15px 10px 15px 10px;
}

.sideTitle b {
    color: rgb(255, 38, 0);
    font-weight: 1000;
    margin-right: 10px;
    line-height: 15px;
}
</style>
